﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Database;
using Business.Utilities;

namespace Business.ConnectDatabase
{
    public class ExpenseCategoryDB
    {
        DataClassesDataContext objDB = new DataClassesDataContext();

        #region Các hàm cơ bản
        public List<tblExpenseCategory> GetAll()
        {
            try
            {
                var list = from a in objDB.tblExpenseCategories where a.Visible select a;
                return list.ToList<tblExpenseCategory>();
            }
            catch (Exception ex)
            {
                SystemLog.Insert(ex);
                throw;
            }
        }

        public tblExpenseCategory GetById(int id)
        {
            try
            {
                var obj = objDB.tblExpenseCategories.SingleOrDefault(a => a.Id == id && a.Visible);
                return obj;
            }
            catch (Exception ex)
            {
                SystemLog.Insert(ex);
                return null;
            }
        }

        public int Insert(tblExpenseCategory obj)
        {
            try
            {
                var exist = objDB.tblExpenseCategories.FirstOrDefault(a => a.Name == obj.Name);
                if (exist == null)
                {
                    objDB.tblExpenseCategories.InsertOnSubmit(obj);
                    objDB.SubmitChanges();
                    return obj.Id;
                }
                else
                {
                    return 0;
                }
            }
            catch (Exception ex)
            {
                SystemLog.Insert(ex);
                return -1;
            }
        }

        public bool Update(tblExpenseCategory obj)
        {
            bool isOk = false;
            try
            {
                var exist = objDB.tblExpenseCategories.SingleOrDefault(a => a.Id == obj.Id);
                if (exist != null)
                {
                    exist.Name = obj.Name;
                    exist.Description = obj.Description;

                    objDB.SubmitChanges();
                    isOk = true;
                }
            }
            catch (Exception ex)
            {
                SystemLog.Insert(ex);
            }
            return isOk;
        }

        public bool Delete(int id)
        {
            bool isOk = false;
            try
            {
                var obj = (from a in objDB.tblExpenseCategories
                           where a.Id == id
                           select a).SingleOrDefault();
                if (obj != null)
                {
                    objDB.tblExpenseCategories.DeleteOnSubmit(obj);
                    objDB.SubmitChanges();
                    isOk = true;
                }
            }
            catch (Exception ex)
            {
                SystemLog.Insert(ex);
            }
            return isOk;
        }

        public bool Hide(int id)
        {
            bool isOk = false;
            try
            {
                var exist = objDB.tblExpenseCategories.SingleOrDefault(a => a.Id == id);
                if (exist != null)
                {
                    exist.Visible = false;
                    objDB.SubmitChanges();
                    isOk = true;
                }
            }
            catch (Exception ex)
            {
                SystemLog.Insert(ex);
            }
            return isOk;
        }

        public bool Restore(int id)
        {
            bool isOk = false;
            try
            {
                var exist = objDB.tblExpenseCategories.SingleOrDefault(a => a.Id == id);
                if (exist != null)
                {
                    exist.Visible = true;
                    objDB.SubmitChanges();
                    isOk = true;
                }
            }
            catch (Exception ex)
            {
                SystemLog.Insert(ex);
            }
            return isOk;
        }
        #endregion
    }
}
